const app = getApp()
import {
    showMsg
} from '../../../../utils/config.js'

import {
    validateEmoji,
    validateSpecial,
    validateMobile
} from '../../../../utils/validate.js'
let spacePopoverSel = require('../behaviors/space/space-popover-sel.js')
import {
    reqalarmReveiverAdd
} from '../../../../request/api'
Page({
    behaviors: [spacePopoverSel], //空间弹窗选择混入
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        form: {
            name: '',
            mobile: '',
            level: 1
        },
        receiveTypeList: [{
            id: 1,
            name: '公众号',
            active: false,
        }, {
            id: 2,
            name: '短信',
            active: false,
        }, {
            id: 0,
            name: '语音',
            active: false,
        },],
        disabledBtn: false,
        showlevel: false,
        levelName: "一级",
        levelList: [{
            id: 1,
            text: "一级"
        }, {
            id: 2,
            text: "二级"
        }, {
            id: 3,
            text: "三级"
        }, {
            id: 4,
            text: "四级"
        }],
    },
    onLoad(e) {
        this.getParkId(e)
    },
    onShow() {
        this.getSpaceList()
    },
    // 获取园区id
    getParkId(e) {
        let params = JSON.parse(e.params)
        console.log('params', params);
        this.setData({
            spaceId: params.spaceId,
            spaceName: params.spaceName
        })
    },
    // 获取名字
    nameChange(e) {
        this.setData({
            'form.name': e.detail.value
        })
    },
    // 清除名字
    nameClear() {
        this.setData({
            'form.name': ''
        })
    },
    // 获取手机号
    mobileChange(e) {
        this.setData({
            'form.mobile': e.detail.value
        })
    },
    // 清除手机号
    mobileClear() {
        this.setData({
            'form.mobile': ''
        })
    },

    // 获取通知方式
    receiveTypeChange(e) {
        let {
            id
        } = e.currentTarget.dataset
        let {
            receiveTypeList
        } = this.data
        receiveTypeList.forEach(item => {
            if (item.id === id) {
                if (e.detail) {
                    item.active = true
                } else {
                    item.active = false
                }
            }
        })
        this.setData({
            receiveTypeList
        })
    },
    // 确认新增
    confirm() {
        if (this.data.disabledBtn) return
        let {
            form,
            spaceId,
            receiveTypeList
        } = this.data
        if (!form.name) return showMsg("请输入真实姓名")
        if (!form.mobile) return showMsg("请输入手机号码")
        if (!validateMobile(form.mobile)) return
        let params = {
            orgId: app.globalData.orgId == 1 ? '' : app.globalData.orgId,
            spaceId,
            name: form.name,
            mobile: form.mobile,
            // mobileReceive: receiveTypeList[0].active ? 1 : 0,
            // mpReceive: receiveTypeList[1].active ? 1 : 0,
            // msgReceive: receiveTypeList[2].active ? 1 : 0,
            mpReceive: receiveTypeList[0].active ? 1 : 0,
            msgReceive: receiveTypeList[1].active ? 1 : 0,
            mobileReceive: receiveTypeList[2].active ? 1 : 0,
            level: spaceId ? form.level : ''
        }
        this.reqAddFun(params)
    },
    // 请求接口
    async reqAddFun(params) {
        const res = await reqalarmReveiverAdd(params)
        if (res.code != 20000) return showMsg(res.message, 'error');
        showMsg("新增成功", "success")
        this.setData({
            disabledBtn: true
        })
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 2000)
    },
    // 弹出
    clicklevel() {
        this.setData({
            showlevel: true
        })
    },
    // 隐藏
    levelClose() {
        this.setData({
            showlevel: false
        })
    },
    // 确定监管等级
    levelConfirm(e) {
        console.log(e);
        this.setData({
            levelName: e.detail.value.text,
            "form.level": e.detail.value.id,
            showlevel: false,
        })
    },

})